package com.wisesoft.ncp.controller;

import com.wisesoft.ncp.entity.VirusEntity;
import com.wisesoft.ncp.service.VirusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by lvchang on 2020/3/9.
 */
@Controller
public class VirusController {

    @Autowired
    private VirusService virusService;

    @RequestMapping(value = "/virus", method = RequestMethod.GET)
    public String getVirusList(Model model)
    {
        List<VirusEntity> all = virusService.findAll();
        model.addAttribute("all", all);
        return "/virus";
    }

    @RequestMapping(value = "/virus/", method = RequestMethod.GET)
    public String getVirusListByNumber(Model model, @RequestParam(value = "number") Integer number)
    {
        List<VirusEntity> all = virusService.findByNumberLessThan(number);
        model.addAttribute("all", all);
        return "/virus";
    }

    @ResponseBody
    @RequestMapping(value = "/virus/{id}", method = RequestMethod.POST)
    public String updateVirus(Model model,
                               @PathVariable(name = "") Long id,
                               VirusEntity virusEntity)
    {
        try {
            virusService.update(virusEntity);
            return "update success!";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "update failed!";
    }
}
